﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using H2.NET.Controls;
using H2.NET.Business;
using H2.NET.Data.Common;

using System.IO;
using H2.NET.Data.Exceller;

namespace H2.NET.Web.Pages.TW_SPM
{
    public partial class T_IcdCustomerMaster : TaskPage
    {
        #region 属性 Public
        public string color
        {
            get
            {
                return VD_Category.SelectedValue == "1" && TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit ? "Blue" : "Black";
            }
        }

        public string color2
        {
            get
            {
                return (VD_Category.SelectedValue == "1" || !TaskAPI.IsBeginStep) && TaskAPI.IsAllowSubmit ? "Black" : "Blue";
            }
        }
        public string color4
        {
            get
            {
                return (VD_Category.SelectedValue != "1" && TaskAPI.IsBeginStep) && TaskAPI.IsAllowSubmit ? "Blue" : "Black";
            }
        }
        public string color5
        {
            get
            {
                return "Blue";
            }
        }
        public string color3
        {
            get
            {
                return TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit ? "Blue" : "Black";
            }
        }

        public string Vendor_Type
        {
            get
            {
                return ViewState["Vendor_Type"] == null ? "" : (string)ViewState["Vendor_Type"];
            }
            set { ViewState["Vendor_Type"] = value; }
        }

        public string Action_Type
        {
            get
            {
                return ViewState["Action_Type"] == null ? "" : (string)ViewState["Action_Type"];
            }
            set { ViewState["Action_Type"] = value; }
        }

        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }
        #endregion

        #region 事件 Page_Load(页面加载)
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                INFO.Text = string.Format("<br/>表单正在被同事“{0}”抢占处理，拒绝提交！", RobUser);
                INFO.Visible = IsOverFlow;

                bool flg = (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit) ? true : false;


                InitDropDownList();

                #region 发起步骤
                if (TaskAPI.IsInitiate)
                {
                    Vendor_Type = "NA";
                    Action_Type = "1";
                }
                #endregion

                #region 审批环节
                else
                {
                    #region 加载数据并显示
                    TW_Icd_Customer mt = (TW_Icd_Customer.process == TaskAPI.Process & TW_Icd_Customer.incident == TaskAPI.Incident).ListFirst<TW_Icd_Customer>();
                    if (mt != null)
                    {
                        ApplicationNo.Text = mt.ApplicationNo;
                        VD_Category.SelectedValue = mt.VD_Category;
                    }
                    //VD_Category.SelectedValue	=	mt.VD_Category;
                    DrpPriority.SelectedValue = mt.DrpPriority;
                    DMO_DrpPriority.SelectedValue = mt.DMO_DrpPriority;
                    Account_Group.Text	=	mt.Account_Group;
                    Existed_Vendor_Code.Text	=	mt.Existed_Vendor_Code;
                    Old_BPCS_Customer_Code.Text	=	mt.Old_BPCS_Customer_Code;
                    Name1.Text	=	mt.Name1;
                    Search_Term1.Text	=	mt.Search_Term1;
                    Search_Term2.Text	=	mt.Search_Term2;
                    Country.Text	=	mt.Country;
                    Region.SelectedValue = mt.Region;
                    Language.SelectedValue = mt.Language;
                    Street.Text	=	mt.Street;
                    Postal_Code.Text	=	mt.Postal_Code;
                    Contact_Person.Text	=	mt.Contact_Person;
                    Telephone.Text	=	mt.Telephone;
                    Email.Text	=	mt.Email;
                    Fax_Number.Text	=	mt.Fax_Number;
                    Transportation_zone.SelectedValue = mt.Transportation_zone;
                    Industry.SelectedValue = mt.Industry;
                    Industry_Code1.SelectedValue = mt.Industry_Code1;
                    Industry_Code2.Text	=	mt.Industry_Code2;
                    Industry_Code3.SelectedValue = mt.Industry_Code3;
                    Bank_Country.Text	=	mt.Bank_Country;
                    Bank_Name.Text	=	mt.Bank_Name;
                    Bank_Key.Text	=	mt.Bank_Key;
                    Bank_Account.Text	=	mt.Bank_Account;
                    Account.Text	=	mt.Account;
                    Company.Text	=	mt.Company;
                    Recon_Account.SelectedValue = mt.Recon_Account;
                    Sort_Key.Text	=	mt.Sort_Key;
                    Dunn_Procedure.Text = mt.Dunn_Procedure;
                    Prev_Acct.Text	=	mt.Prev_Acct;
                    Payt_Terms.SelectedValue = mt.Payt_Terms;
                    Payment_Methods.SelectedValue = mt.Payment_Methods;
                    Payment_history_record.Checked	=	mt.Payment_history_record=="1";
                    Bank_statement.Text	=	mt.Bank_statement;
                    Sales_Org.Text	=	mt.Sales_Org;
                    Distribution_Channel.Text	=	mt.Distribution_Channel;
                    Customer_Division.Text	=	mt.Customer_Division;
                    Sales_Office.SelectedValue = mt.Sales_Office;
                    Order_Currency.SelectedValue	=	mt.Order_Currency;
                    Switch_off_rounding.Checked	=	mt.Switch_off_rounding=="1";
                    Cust_pric.SelectedValue = mt.Cust_pric;
                    Delivery_Priority.SelectedValue = mt.Delivery_Priority;
                    Shipping_Conditions.SelectedValue = mt.Shipping_Conditions;
                    Order_Combination.Checked	=	mt.Order_Combination=="1";
                    Delivering_Plant.SelectedValue = mt.Delivering_Plant;
                    Relevant_for_POD.Checked	=	mt.Relevant_for_POD=="1";
                    Max_partial_deliveries.Text	=	mt.Max_partial_deliveries;
                    Rebat.Checked	=	mt.Rebat=="1";
                    Incoterms.SelectedValue = mt.Incoterms;
                    Price_detemin.Checked	=	mt.Price_detemin=="1";
                    Incoterms2.Text	=	mt.Incoterms2;
                    Terms_of_payment.SelectedValue = mt.Terms_of_payment;
                    Complete_delivery.Text	=	mt.Complete_delivery;
                    Partial_delivery.Text	=	mt.Partial_delivery;
                    Acct_assgmt_group.Text	=	mt.Acct_assgmt_group;
                    Departing_country.Text	=	mt.Departing_country;
                    Tax_classification.Text	=	mt.Tax_classification;
                    Customer_grp.Text	=	mt.Customer_grp;
                    Text_Internal.Text	=	mt.Text_Internal;
                    Text_Shipping.Text	=	mt.Text_Shipping;
                    Text_Pay.Text	=	mt.Text_Pay;
                    Text_External.Text	=	mt.Text_External;
                    Attributes1.SelectedValue = mt.Attributes1;
                    Attributes2.SelectedValue = mt.Attributes2;
                    Attributes3.SelectedValue = mt.Attributes3;
                    Attributes4.SelectedValue = mt.Attributes4;
                    Attributes5.SelectedValue = mt.Attributes5;
                    Attributes6.SelectedValue = mt.Attributes6;
                    Attributes7.SelectedValue = mt.Attributes7;
                    Attributes8.SelectedValue = mt.Attributes8;
                    Attributes9.SelectedValue = mt.Attributes9;
                    SAP_Code.Text = mt.SAP_Code;
                    APO.SelectedValue = mt.APO;

                    #endregion

                    #region 审批项初始化
                    switch (TaskAPI.Step)
                    {
                        case "Applicant Confirm":
                        case "DMO Maintain":
                        case "DMO Check":
                        case "LAM Approve":
                            Assessment1.Option.Add(new t_AssessmentOption("Finish", "1", true));
                            Assessment1.Option.Add(new t_AssessmentOption("Return", "2", false));
                            break;
                        case "Accounting Manager Approve":
                        case "Finance Manager Approve":
                        case "DMO Approve":
                        case "Manager Approve":
                            Assessment1.Option.Add(new t_AssessmentOption("Approve", "1", true));
                            Assessment1.Option.Add(new t_AssessmentOption("Return", "2", false));
                            break;
                    }
                    #endregion
                }
                #endregion

                #region 设置控件禁启用
                //SetEnable(flg, VD_Chk, VD_GR_Based, VD, SA, SA2, VD_YY, SA2_Sales_Office, VD_Company, VD_Vendor_Type, VD_Search_Term, VD_Street, VD_Country, VD_City, VD_District, VD_Postal_Code, VD_Language, VD_Contact_Person, VD_Telephone, VD_Ext_1, VD_Fax_Number, VD_Ext_2, VD_Email, VD_Bank_Country, VD_Bank_Name, VD_Bank_Account, VD_Recon_Account, VD_Cash_Mgmnt_Group, VD_Payt_Terms, VD_Payment_Methods, VD_Payment_Terms, VD_Order_Currency, VD_Purchasing_Group, VD_Category, VD_Area1, VD_Area2, VD_Area3, VD_Vendor_Master_Code, VD_Customer_sold_to_code, SA_Vendor_Master_Code, SA_Name, SA_Company, SA_Nationality, SA_Telephone, SA_Ext_3, SA_Cell_Phone, SA_Fax_Number, SA_Email, SA_Bank_Name, SA_Bank_Country, SA_Bank_Account, SA2_Personnel_NO, SA2_From_Date, SA2_Personnel_Area, SA2_EE_Group, SA2_Subarea, SA2_Last_Name, SA2_First_Name, SA2_Gender, SA2_Birth_Date, SA2_Street_And_HseNo, SA2_City, SA2_Mobile_Phone_Number, SA2_EMail_Address, SAP_Code);
                SetEnable(TaskAPI.Step == "DMO Maintain", SAP_Code);
                SetEnable(TaskAPI.Step == "DMO Approve" | TaskAPI.Step == "DMO Maintain" | TaskAPI.Step == "DMO Check", DMO_DrpPriority);
                SetVisible(TaskAPI.Step == "DMO Approve" | TaskAPI.Step == "DMO Maintain" | TaskAPI.Step == "DMO Check", P_DMO);
                SetEnable(flg, P_VD, DrpPriority);
                bool flg1 = (TaskAPI.IsBeginStep) ? true : false;

                Assessment1.Enable = TaskAPI.IsAllowSubmit;
                SetVisible(TaskAPI.IsAllowSubmit, Send, Split);
                Attachment1.AllowDelete = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
                Attachment1.AllowUpload = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
                if (VD_Category.SelectedValue == "1")
                {
                    SetVisible(false, Existed_Vendor_Code_L, Existed_Vendor_Code);

                }
                else
                {
                    SetVisible(true, Existed_Vendor_Code_L, Existed_Vendor_Code);
                }
                #endregion

                #region 颜色加深
                if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit)
                {
                    if (VD_Category.SelectedValue == "1")
                    {
                        SetForeColor(Color.Blue, Old_BPCS_Customer_Code_L, Name1_L, Search_Term1_L, Country_L, Language_L, Street_L, Transportation_zone_L, Recon_Account_L, Payment_Methods_L, Payt_Terms_L, Order_Currency_L,Sales_Office_L, Shipping_Conditions_L, Delivering_Plant_L, Incoterms_L, Terms_of_payment_L, Acct_assgmt_group_L, Customer_grp_L, Cust_pric_L);
                    }
                    else
                    {
                        SetForeColor(Color.Blue, Name1_L, Existed_Vendor_Code_L, Sales_Office_L);
                    }
                }

                if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit)
                {
                    foreach (Control cr in P_VD.Controls)
                    {
                        if (cr is TextBox)
                        {
                            TextBox tb = (TextBox)cr;
                            string color = tb.ID.ToString() + "_L";
                            Label lb = (Label)this.P_VD.FindControl(color);
                            if (lb != null && !string.IsNullOrEmpty(tb.Text))
                            {
                                lb.ForeColor = System.Drawing.Color.Orange;
                            }
                        }
                    }
                    foreach (Control cr in P_VD.Controls)
                    {
                        if (cr is DropDownList)
                        {
                            DropDownList tb = (DropDownList)cr;
                            if (!tb.Enabled)
                            {
                                string color = tb.ID.ToString() + "_L";
                                Label lb = (Label)this.P_VD.FindControl(color);
                                if (lb != null && !string.IsNullOrEmpty(tb.Text))
                                {
                                    lb.ForeColor = System.Drawing.Color.Orange;
                                }
                            }
                        }
                    }

                }
                #endregion
            }
        }
        #endregion

        #region 事件 Send
        protected void Send_Click(object sender, EventArgs e)
        {
            try
            {
                #region 数据验证
                if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit) // 如果是退回的情况，不存在验证
                {
                    List<string> vald_s = new List<string>();
                    vald_s.AddRange(DataValidation());
                    if (vald_s.Count > 0)
                    {
                        Anthem.Manager.AddScriptForClientSideEval(string.Format("ShowMessage(\"{0}\", {1});", CombineMessage(vald_s), vald_s.Count));
                        return;
                    }
                }
                #endregion

                #region 引擎传值
                if (TaskAPI.IsBeginStep)
                {
                    if (TaskAPI.IsInitiate)
                    {
                        ApplicationNo.Text = Util.GenerateSequence(((object[])TaskAPI["PrefixNo"])[0].ToString() + "-" + System.DateTime.Now.ToString("yyMM"), "", 4);
                        TaskAPI.UserName = UserAPI.UserName;
                        TaskAPI["ApplicationNo"] = ApplicationNo.Text;
                        TaskAPI["ApplicatorName"] = UserAPI.UserFullName;
                        TaskAPI["Applicator"] = UserAPI.UserName;
                        TaskAPI["Time"] = System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss");
                        TaskAPI["ApplicatorEmail"] = UserAPI.Mail;
                        TaskAPI["Department"] = UserAPI.Department;
                        TaskAPI["ServerName"] = Util.AppSettings("ServerName");
                    }

                    TaskAPI.Summary = (VD_Category.SelectedItem.Text.Split(',').Length >= 2 ? VD_Category.SelectedItem.Text.Split(',')[1].Trim() : VD_Category.Text)
                                    + " / " + ApplicationNo.Text;

                    //TaskAPI["Vendor_Type"] = VD.Checked && VD_Vendor_Type.SelectedValue == "内部(Internal)" ? "Internal" : "";
                    TaskAPI["Actions"] = VD_Category.SelectedValue;
                    TaskAPI["Remark_by_Initiator"] = Assessment1.Assessment.Remark;
                    TaskAPI["DrpPriority"] = DrpPriority.SelectedItem.Text;

                    #region 从数据库取得流程步骤处理人
                    SortedList<string, string> rec_a = new SortedList<string, string>();

                    IList<mdc_Recipient> rec_s = (mdc_Recipient.process == TaskAPI.Process & (mdc_Recipient.distribute == VD_Category.SelectedValue | mdc_Recipient.distribute == "*") & mdc_Recipient.status == 1).List<mdc_Recipient>();
                    foreach (mdc_Recipient rec in rec_s)
                    {
                        if (!rec_a.ContainsKey(rec.Step))
                        {
                            rec_a.Add(rec.Step, rec.Recipient);
                            continue;
                        }

                        if (rec_a.Values[rec_a.IndexOfKey(rec.Step)].Contains(rec.Recipient))
                        {
                            continue;
                        }

                        rec_a[rec.Step] = rec_a.Values[rec_a.IndexOfKey(rec.Step)] + "," + rec.Recipient;
                    }

                    //步骤处理人赋值
                    for (int i = 0; i < rec_a.Keys.Count; i++)
                    {
                        TaskAPI["R_" + rec_a.Keys[i].Replace(" ", "_")] = rec_a.Values[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    }

                    IList<m_TW_Choose> tw_choose = (m_TW_Choose.recipient == TaskAPI.UserName & m_TW_Choose.status == 1).List<m_TW_Choose>();
                    if (tw_choose.Count == 0)
                    {
                        TaskAPI["R_Manager_Approve"] = null;
                        m_OrgChart oc = (m_OrgChart.empID.In(m_OrgChart.supervisorEmpID, m_OrgChart.winID == UserAPI.UserName)).ListFirst<m_OrgChart>();
                        if (oc != null)
                        {
                            TaskAPI["R_Manager_Approve"] = oc.WinID;
                        }
                    }
                     

                     
                    #endregion
                }
                else
                {
 
                    TaskAPI["ApproveResult"] = Assessment1.Assessment.Opinion;
                    TaskAPI["TaskID"] = TaskAPI.TaskID;
                }

                
                #endregion

                #region 提交步骤
                bool flg = false;
                Storage.TransactionExecute(o =>
                {
                    #region “Fill in the form”步骤
                    if (TaskAPI.IsBeginStep)
                    {

                        TW_Icd_Customer mt = new TW_Icd_Customer();
                        if ((TW_Icd_Customer.process == TaskAPI.Process & TW_Icd_Customer.incident == TaskAPI.Incident & TW_Icd_Customer.userName == UserAPI.UserName).Count<TW_Icd_Customer>() > 0)
                        {
                            mt = (TW_Icd_Customer.process == TaskAPI.Process & TW_Icd_Customer.incident == TaskAPI.Incident & TW_Icd_Customer.userName == UserAPI.UserName).ListFirst<TW_Icd_Customer>();
                        }
                        else
                        {
                            mt.Process = TaskAPI.Process;
                            mt.Incident = TaskAPI.Incident;
                            mt.UserName = UserAPI.UserName;
                            mt.UserFullName = UserAPI.UserFullName;
                            mt.ApplicationNo = ApplicationNo.Text;
                            mt.Time = System.DateTime.Now;
                            mt.Status = 0;
                            mt.Department = UserAPI.Department;
                        }

                        mt.VD_Category = VD_Category.SelectedValue;
                        mt.DrpPriority = DrpPriority.SelectedValue;
                        mt.Account_Group = Account_Group.Text;
                        mt.Existed_Vendor_Code = Existed_Vendor_Code.Text;
                        mt.Old_BPCS_Customer_Code = Old_BPCS_Customer_Code.Text;
                        mt.Name1 = Name1.Text;
                        mt.Search_Term1 = Search_Term1.Text;
                        mt.Search_Term2 = Search_Term2.Text;
                        mt.Country = Country.Text;
                        mt.Region = Region.SelectedValue;
                        mt.Language = Language.SelectedValue;
                        mt.Street = Street.Text;
                        mt.Postal_Code = Postal_Code.Text;
                        mt.Contact_Person = Contact_Person.Text;
                        mt.Telephone = Telephone.Text;
                        mt.Email = Email.Text;
                        mt.Fax_Number = Fax_Number.Text;
                        mt.Transportation_zone = Transportation_zone.SelectedValue;
                        mt.Industry = Industry.SelectedValue;
                        mt.Industry_Code1 = Industry_Code1.SelectedValue;
                        mt.Industry_Code2 = Industry_Code2.Text;
                        mt.Industry_Code3 = Industry_Code3.SelectedValue;
                        mt.Bank_Country = Bank_Country.Text;
                        mt.Bank_Name = Bank_Name.Text;
                        mt.Bank_Key = Bank_Key.Text;
                        mt.Bank_Account = Bank_Account.Text;
                        mt.Account = Account.Text;
                        mt.Company = Company.Text;
                        mt.Recon_Account = Recon_Account.SelectedValue;
                        mt.Sort_Key = Sort_Key.Text;
                        mt.Dunn_Procedure = Dunn_Procedure.Text;
                        mt.Prev_Acct = Prev_Acct.Text;
                        mt.Payt_Terms = Payt_Terms.SelectedValue;
                        mt.Payment_Methods = Payment_Methods.SelectedValue;
                        mt.Payment_history_record = Payment_history_record.Checked ? "1" : "0";
                        mt.Bank_statement = Bank_statement.Text;
                        mt.Sales_Org = Sales_Org.Text;
                        mt.Distribution_Channel = Distribution_Channel.Text;
                        mt.Customer_Division = Customer_Division.Text;
                        mt.Sales_Office = Sales_Office.SelectedValue;
                        mt.Order_Currency = Order_Currency.SelectedValue;
                        mt.Switch_off_rounding = Switch_off_rounding.Checked ? "1" : "0";
                        mt.Cust_pric = Cust_pric.SelectedValue;
                        mt.Delivery_Priority = Delivery_Priority.SelectedValue;
                        mt.Shipping_Conditions = Shipping_Conditions.SelectedValue;
                        mt.Order_Combination = Order_Combination.Checked ? "1" : "0";
                        mt.Delivering_Plant = Delivering_Plant.SelectedValue;
                        mt.Relevant_for_POD = Relevant_for_POD.Checked ? "1" : "0";
                        mt.Max_partial_deliveries = Max_partial_deliveries.Text;
                        mt.Rebat = Rebat.Checked ? "1" : "0";
                        mt.Incoterms = Incoterms.SelectedValue;
                        mt.Price_detemin = Price_detemin.Checked ? "1" : "0";
                        mt.Incoterms2 = Incoterms2.Text;
                        mt.Terms_of_payment = Terms_of_payment.SelectedValue;
                        mt.Complete_delivery = Complete_delivery.Text;
                        mt.Partial_delivery = Partial_delivery.Text;
                        mt.Acct_assgmt_group = Acct_assgmt_group.Text;
                        mt.Departing_country = Departing_country.Text;
                        mt.Tax_classification = Tax_classification.Text;
                        mt.Customer_grp = Customer_grp.Text;
                        mt.Text_Internal = Text_Internal.Text;
                        mt.Text_Shipping = Text_Shipping.Text;
                        mt.Text_Pay = Text_Pay.Text;
                        mt.Text_External = Text_External.Text;
                        mt.Attributes1 = Attributes1.SelectedValue;
                        mt.Attributes2 = Attributes2.SelectedValue;
                        mt.Attributes3 = Attributes3.SelectedValue;
                        mt.Attributes4 = Attributes4.SelectedValue;
                        mt.Attributes5 = Attributes5.SelectedValue;
                        mt.Attributes6 = Attributes6.SelectedValue;
                        mt.Attributes7 = Attributes7.SelectedValue;
                        mt.Attributes8 = Attributes8.SelectedValue;
                        mt.Attributes9 = Attributes9.SelectedValue;
                        mt.SAP_Code = SAP_Code.Text;
                        mt.APO = APO.SelectedValue;

                        (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == 0 & t_Attachment.username == UserAPI.UserName).Edit<t_Attachment>(p =>
                        {
                            p.Incident = TaskAPI.Incident;
                        });

                        Storage.Save(Storage.Context1, mt);
                    }
                    #endregion

                    #region Priority功能
                    if (TaskAPI.IsBeginStep && !string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {
                        mdc_Priority pro;
                        if ((mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).Count<mdc_Priority>() > 0)
                        {
                            pro = new mdc_Priority();
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.Business_Priority = Convert.ToInt32(DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }
                        else
                        {
                            pro = new mdc_Priority();
                            pro.Process = TaskAPI.Process;
                            pro.Incident = TaskAPI.Incident;
                            pro.Business_Priority = Convert.ToInt32(DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }

                    }


                    if ((TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Approve" || TaskAPI.Step == "DMO Check") && !string.IsNullOrEmpty(this.DMO_DrpPriority.SelectedValue) && !string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {

                        //mdc_Priority pro;
                        //pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                        //pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                        //Storage.Save(pro);
                        mdc_Priority pro;
                        if ((mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).Count<mdc_Priority>() > 0)
                        {
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }
                        else
                        {
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }


                    }
                    #endregion

                    #region “DMO Maintain”步骤
                    if (TaskAPI.Step == "DMO Maintain")
                    {
                        (TW_Icd_Customer.process == TaskAPI.Process & TW_Icd_Customer.incident == TaskAPI.Incident).Edit<TW_Icd_Customer>(p =>
                        {
                            p.SAP_Code = SAP_Code.Text;
                        }
                        );
                    }
                    if (TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Approve" || TaskAPI.Step == "DMO Check")
                    {

                        TaskAPI["DMO_DrpPriority"] = DMO_DrpPriority.SelectedItem.Text;
                        (TW_Icd_Customer.process == TaskAPI.Process & TW_Icd_Customer.incident == TaskAPI.Incident).Edit<TW_Icd_Customer>(p =>
                        {
                            // p.SAP_Code = SAP_Code.Text;
                            p.DMO_DrpPriority = DMO_DrpPriority.SelectedValue;

                        }
                        );
                    }
                    #endregion

                    Storage.Save(Assessment1.Assessment);    //审批意见  
                });

                //多节点退回
                if (!string.IsNullOrEmpty(Assessment1.JumpStep))
                {
                    Assessment1.SetAsReturn(true);
                    TaskAPI.DirectActive(Assessment1.JumpStep);
                }
                else
                {
                    Assessment1.SetAsReturn(false);
                }
                Assessment1.SubmitTo();

                flg = TaskAPI.Send();

                //如果数据库写入成功且流程发起成功的情况，将业务表数据与流程实例号进行关联
                if (TaskAPI.IsInitiate)
                {
                    if (!string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {
                        (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == 0).Edit<mdc_Priority>(p =>
                        {
                            p.Incident = TaskAPI.Incident;
                        });
                    }
                    (TW_Icd_Customer.process == TaskAPI.Process & TW_Icd_Customer.incident == 0 & TW_Icd_Customer.userName == UserAPI.UserName).Edit<TW_Icd_Customer>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (t_Assessment.process == TaskAPI.Process & t_Assessment.incident == 0 & t_Assessment.assignedToUserName == UserAPI.UserName).Edit<t_Assessment>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == 0 & t_Attachment.username == UserAPI.UserName).Edit<t_Attachment>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });
                }
                #endregion

                Send.Visible = false;
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('" + Resources.Global.C_00_Success + "',true)");
            }
            catch (Exception ex)
            {
                TaskDB.Log("", false, "调用NVA方法Send_Click出错：" + ex.ToString());
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('" + Resources.Global.C_00_Failed + "',false)");
            }
        }
        #endregion


        #region 事件 Old_BPCS_Customer_Code_CheckedChanged 
        protected void Old_BPCS_Customer_Code_TextChanged(object sender, EventArgs e)
        {
            this.Search_Term2.Text = this.Old_BPCS_Customer_Code.Text;
            this.Prev_Acct.Text = this.Old_BPCS_Customer_Code.Text;
        }
        #endregion
        #region 事件 Region_SelectedIndexChanged
        protected void Region_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Attributes9.SelectedValue = this.Region.SelectedValue;
        }
        #endregion
        #region 事件 Category变换
        protected void VD_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (VD_Category.SelectedValue == "1")
            {
                this.Old_BPCS_Customer_Code_L.Text = "ICD Customer code:";
                SetVisible(false, Existed_Vendor_Code_L, Existed_Vendor_Code);
                SetForeColor(Color.Black, Name1_L, Existed_Vendor_Code_L);
                SetForeColor(Color.Blue, Name1_L, Old_BPCS_Customer_Code_L, Search_Term1_L, Country_L, Language_L, Street_L, Transportation_zone_L, Recon_Account_L, Payment_Methods_L, Payt_Terms_L, Sales_Office_L,Order_Currency_L, Shipping_Conditions_L, Delivering_Plant_L, Incoterms_L, Terms_of_payment_L, Acct_assgmt_group_L, Customer_grp_L, Cust_pric_L);
            }
            else
            {
                this.Old_BPCS_Customer_Code_L.Text = "Old BPCS Customer Code:";
                SetVisible(true, Existed_Vendor_Code_L, Existed_Vendor_Code);
                SetForeColor(Color.Black, Old_BPCS_Customer_Code_L, Name1_L, Search_Term1_L, Country_L, Language_L, Street_L, Transportation_zone_L, Recon_Account_L, Payment_Methods_L, Payt_Terms_L, Order_Currency_L, Shipping_Conditions_L, Delivering_Plant_L, Incoterms_L, Terms_of_payment_L, Acct_assgmt_group_L, Customer_grp_L, Cust_pric_L);

                SetForeColor(Color.Blue, Name1_L, Existed_Vendor_Code_L, Sales_Office_L);
            }
        }
        #endregion

    
        #region Payt_Terms
        protected void VD_Payt_Terms_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch ((sender as Anthem.DropDownList).ID)
            {
                case "VD_Payt_Terms":
               
                    break;

                case "VD_Payment_Terms":
 
                    break;
            }
        }
        #endregion

      
 
        #region 方法 InitDropdownList
        private void InitDropDownList()
        {
            if (!IsLoaded)
            {

                Region.Items.Clear();
                Region.Items.Add(new ListItem("", ""));
                IList<m_TW_Region> region = (m_TW_Region.status == 1).List<m_TW_Region>();
                foreach (m_TW_Region mr in region)
                {
                    Region.Items.Add(new ListItem(mr.TW_Region, mr.TW_Region));
                }

                Transportation_zone.Items.Clear();
                Transportation_zone.Items.Add(new ListItem("", ""));
                IList<m_Transportation_Zone> tzone = (m_Transportation_Zone.status == 1).List<m_Transportation_Zone>();
                foreach (m_Transportation_Zone mz in tzone)
                {
                    Transportation_zone.Items.Add(new ListItem(mz.Transportation_Zone + "  " + mz.Description, mz.Transportation_Zone));
                }

                Industry.Items.Clear();
                Industry.Items.Add(new ListItem("", ""));
                IList<m_TW_Industry_Global> industry = (m_TW_Industry_Global.status == 1).List<m_TW_Industry_Global>();
                foreach (m_TW_Industry_Global id in industry)
                {
                    Industry.Items.Add(new ListItem(id.TW_Industry_Global, id.TW_Industry_Global));
                }

                Industry_Code1.Items.Clear();
                Industry_Code1.Items.Add(new ListItem("", ""));
                IList<m_TW_Industry_Code1_Local> industry1 = (m_TW_Industry_Code1_Local.status == 1).List<m_TW_Industry_Code1_Local>();
                foreach (m_TW_Industry_Code1_Local id1 in industry1)
                {
                    Industry_Code1.Items.Add(new ListItem(id1.Industry_Code1_Local, id1.Industry_Code1_Local));
                }

                Industry_Code3.Items.Clear();
                Industry_Code3.Items.Add(new ListItem("", ""));
                Industry_Code3.Items.Add(new ListItem("3ZZ-0162 家樂福  Carrefour", "3ZZ-0162 家樂福  Carrefour"));
                Industry_Code3.Items.Add(new ListItem("3TW-0001 普來利   Homebox", "3TW-0001 普來利   Homebox"));

                Recon_Account.Items.Clear();
                Recon_Account.Items.Add(new ListItem("", ""));
                IList<m_TW_Reco_Account> recon = (m_TW_Reco_Account.status == 1).List<m_TW_Reco_Account>();
                foreach (m_TW_Reco_Account ra in recon)
                {
                    Recon_Account.Items.Add(new ListItem(ra.Recon_Account + " " + ra.Recon_Description, ra.Recon_Account));
                }

                Payment_Methods.Items.Clear();
                Payment_Methods.Items.Add(new ListItem("", ""));
                IList<m_TW_Payment_Methods> pMethods = (m_TW_Payment_Methods.status == 1).List<m_TW_Payment_Methods>();
                foreach (m_TW_Payment_Methods pm in pMethods)
                {
                    Payment_Methods.Items.Add(new ListItem(pm.Payment_Methods, pm.Payment_Methods));
                }

                Payt_Terms.Items.Clear();
                Payt_Terms.Items.Add(new ListItem("", ""));
                IList<m_TW_Payment_Terms> pTerms = (m_TW_Payment_Terms.status == 1).List<m_TW_Payment_Terms>();
                foreach (m_TW_Payment_Terms pt in pTerms)
                {
                    Payt_Terms.Items.Add(new ListItem(pt.Payment_Terms, pt.Payment_Terms));
                }

                Delivery_Priority.Items.Clear();
                Delivery_Priority.Items.Add(new ListItem("", ""));
                IList<m_TW_Delivery_Priority> dPriority = (m_TW_Delivery_Priority.status == 1).List<m_TW_Delivery_Priority>();
                foreach (m_TW_Delivery_Priority dp in dPriority)
                {
                    Delivery_Priority.Items.Add(new ListItem(dp.Delivery_Priority, dp.Delivery_Priority));
                }

                Shipping_Conditions.Items.Clear();
                Shipping_Conditions.Items.Add(new ListItem("", ""));
                IList<m_TW_Shipping_Conditions> sConditions = (m_TW_Shipping_Conditions.status == 1).List<m_TW_Shipping_Conditions>();
                foreach (m_TW_Shipping_Conditions sc in sConditions)
                {
                    Shipping_Conditions.Items.Add(new ListItem(sc.Shipping_Conditions, sc.Shipping_Conditions));
                }

                Delivering_Plant.Items.Clear();
                Delivering_Plant.Items.Add(new ListItem("", ""));
                Delivering_Plant.Items.Add(new ListItem("KT2D PKG", "KT2D PKG"));
                Delivering_Plant.Items.Add(new ListItem("KT1D DECO", "KT1D DECO"));

                Incoterms.Items.Clear();
                Incoterms.Items.Add(new ListItem("", ""));
                IList<m_TW_Incoterms> incoterms = (m_TW_Incoterms.status == 1).List<m_TW_Incoterms>();
                foreach (m_TW_Incoterms inc in incoterms)
                {
                    Incoterms.Items.Add(new ListItem(inc.Incoterms, inc.Incoterms));
                }


                Terms_of_payment.Items.Clear();
                Terms_of_payment.Items.Add(new ListItem("", ""));
                IList<m_TW_Payment_Terms> tPayment = (m_TW_Payment_Terms.status == 1).List<m_TW_Payment_Terms>();
                foreach (m_TW_Payment_Terms tp in tPayment)
                {
                    Terms_of_payment.Items.Add(new ListItem(tp.Payment_Terms, tp.Payment_Terms));
                }

                Attributes1.Items.Clear();
                Attributes1.Items.Add(new ListItem("", ""));
                IList<m_TW_Attributes1> ta1 = (m_TW_Attributes1.status == 1).List<m_TW_Attributes1>();
                foreach (m_TW_Attributes1 ta in ta1)
                {
                    Attributes1.Items.Add(new ListItem(ta.TW_Attributes1, ta.TW_Attributes1));
                }

                Attributes2.Items.Clear();
                Attributes2.Items.Add(new ListItem("", ""));
                IList<m_TW_Attributes2> ta2 = (m_TW_Attributes2.status == 1).List<m_TW_Attributes2>();
                foreach (m_TW_Attributes2 ta in ta2)
                {
                    Attributes2.Items.Add(new ListItem(ta.TW_Attributes2, ta.TW_Attributes2));
                }

                Attributes3.Items.Clear();
                Attributes3.Items.Add(new ListItem("", ""));
                IList<m_TW_Attributes3> ta3 = (m_TW_Attributes3.status == 1).List<m_TW_Attributes3>();
                foreach (m_TW_Attributes3 ta in ta3)
                {
                    Attributes3.Items.Add(new ListItem(ta.TW_Attributes3, ta.TW_Attributes3));
                }

                Attributes4.Items.Clear();
                Attributes4.Items.Add(new ListItem("", ""));
                IList<m_TW_Attributes4> ta4 = (m_TW_Attributes4.status == 1).List<m_TW_Attributes4>();
                foreach (m_TW_Attributes4 ta in ta4)
                {
                    Attributes4.Items.Add(new ListItem(ta.TW_Attributes4, ta.TW_Attributes4));
                }

                Attributes5.Items.Clear();
                Attributes5.Items.Add(new ListItem("", ""));
                IList<m_TW_Attributes5> ta5 = (m_TW_Attributes5.status == 1).List<m_TW_Attributes5>();
                foreach (m_TW_Attributes5 ta in ta5)
                {
                    Attributes5.Items.Add(new ListItem(ta.TW_Attributes5, ta.TW_Attributes5));
                }

                Attributes6.Items.Clear();
                Attributes6.Items.Add(new ListItem("", ""));
                //IList<m_TW_Attributes5> ta6 = (m_TW_Attributes1.status == 1).List<m_TW_Attributes1>();
                //foreach (m_TW_Attributes1 ta in ta6)
                //{
                //    Attributes6.Items.Add(new ListItem(ta.TW_Attributes1, ta.TW_Attributes1));
                //}

                Attributes7.Items.Clear();
                Attributes7.Items.Add(new ListItem("", ""));
                IList<m_TW_Attributes7> ta7 = (m_TW_Attributes7.status == 1).List<m_TW_Attributes7>();
                foreach (m_TW_Attributes7 ta in ta7)
                {
                    Attributes7.Items.Add(new ListItem(ta.Attributes7, ta.Attributes7));
                }

                Attributes8.Items.Clear();

                Attributes9.Items.Clear();
                Attributes9.Items.Add(new ListItem("", ""));
                IList<m_TW_Region> ta9 = (m_TW_Region.status == 1).List<m_TW_Region>();
                foreach (m_TW_Region ta in ta9)
                {
                    Attributes9.Items.Add(new ListItem(ta.TW_Region, ta.TW_Region));
                }


                Cust_pric.Items.Clear();
                Cust_pric.Items.Add(new ListItem("", ""));
                IList<m_TW_Cust_Pric_Proc> h12 = (m_TW_Cust_Pric_Proc.status == 1).List<m_TW_Cust_Pric_Proc>();
                foreach (m_TW_Cust_Pric_Proc HY in h12)
                {
                    Cust_pric.Items.Add(new ListItem(HY.Cust_Pric_Proc, HY.Cust_Pric_Proc));
                    
                }


                Sales_Office.Items.Clear();
                Sales_Office.Items.Add(new ListItem("", ""));
                IList<m_TW_ICD_Sales_Office> h13 = (m_TW_ICD_Sales_Office.status == 1).List<m_TW_ICD_Sales_Office>();
                foreach (m_TW_ICD_Sales_Office HY in h13)
                {
                    Sales_Office.Items.Add(new ListItem(HY.Sales_Office, HY.Sales_Office));
                    
                }
                IsLoaded = true;
            }
        }
        #endregion

        #region 方法 数据验证
        private List<string> DataValidation()
        {
            #region VD
            List<Regular> regs = new List<Regular>();
            if (VD_Category.SelectedValue == "1")
            {
                regs.Add(new Regular(RegularExp.NotEmpty, Name1.Text.Trim(), "Name 1 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Old_BPCS_Customer_Code.Text.Trim(), "ICD Customer code 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Search_Term1.Text.Trim(), "Search Term1 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Country.Text.Trim(), "Country 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Language.Text.Trim(), "Language 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Street.Text.Trim(), "Street/House No(詳細地址): 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Transportation_zone.SelectedValue.Trim(), "Transportation zone: 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Recon_Account.SelectedValue.Trim(), "Recon. Account(統馭科目): 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Payment_Methods.SelectedValue.Trim(), "Payment Methods(支付方法): 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Payt_Terms.SelectedValue.Trim(), "Payment Terms(付款條件): 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Sales_Office.SelectedValue, "Sales Office: 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Order_Currency.SelectedValue, "Currency(幣種): 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Cust_pric.SelectedValue, "Cust.pric.proc(客戶定價過程): 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Shipping_Conditions.SelectedValue.Trim(), "Shipping Conditions(運輸條件): 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Delivering_Plant.SelectedValue.Trim(), "Delivering Plant: 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Incoterms.SelectedValue.Trim(), "Incoterms: 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Terms_of_payment.SelectedValue.Trim(), "Terms of payment(付款條件): 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Acct_assgmt_group.Text.Trim(), "Acct assgmt group: 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Customer_grp.Text.Trim(), "Customer grp 3(PO No): 必填！"));
      
            }
            else
            {
                regs.Add(new Regular(RegularExp.NotEmpty, Name1.Text.Trim(), "Name 1 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Existed_Vendor_Code.Text.Trim(), "Existed Customer Code: 必填！"));
                regs.Add(new Regular(RegularExp.NotEmpty, Sales_Office.SelectedValue, "Sales Office: 必填！"));
            }
            #endregion
            //魏军添加 将SAP Code设置为必填
            if (TaskAPI.Step == "DMO Maintain")
            {
                regs.Add(new Regular(RegularExp.NotEmpty, SAP_Code.Text.Trim(), "SAP Code 必填！"));
            }

            return Validation(regs);
        }
        #endregion

        #region 方法 Others
        protected void VD_Payment_Methods_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void VD_Name_TextChanged(object sender, EventArgs e)
        { 
        }
        protected void SA_Name_TextChanged(object sender, EventArgs e)
        {
            
        }
        protected void VD_Vendor_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        private string getFormCheckList(Anthem.CheckBoxList ck)
        {
            string t = "";
            for (int i = 0; i < ck.Items.Count; i++)
                if (ck.Items[i].Selected)
                    t += "," + ck.Items[i].Value;
                else
                    t += ",";
            return t;
        }
        private Anthem.CheckBoxList setCheckList(string t, Anthem.CheckBoxList ck)
        {
            try
            {
                string[] tt = t.Split(',');

                for (int i = 0; i <= tt.GetUpperBound(0); i++)
                {
                    for (int j = 0; j < ck.Items.Count; j++)
                    {
                        if (ck.Items[j].Value == tt[i].ToString())
                        { ck.Items[j].Selected = true; break; }
                    }
                }
            }
            catch { }
            return ck;
        }
        #endregion

        #region 方法 SetDropDownListValue
        private void SetDropDownListValue(Anthem.DropDownList drpT, object o)
        {
            try
            {
                //if ((TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit) || drpT.Items.FindByValue(o.ToString()) != null)
                //{
                //    drpT.SelectedValue = o.ToString();
                //    return;
                //}
                // drpT.Items.Add(new ListItem(o.ToString(), o.ToString()));
                // drpT.SelectedValue = o.ToString();
                //魏军为了修改下拉框的enable属性而改。

                if ((!TaskAPI.IsBeginStep && TaskAPI.Step != "CA Check") || !TaskAPI.IsAllowSubmit)
                {
                    drpT.SelectedValue = o.ToString();
                    string item = drpT.SelectedItem.Text;
                    drpT.Items.Clear();
                    drpT.Items.Add(new ListItem(item, o.ToString()));
                    drpT.SelectedValue = o.ToString();

                }
                else
                {
                    if (drpT.Items.FindByValue(o.ToString().Trim()) != null)
                    {
                        drpT.SelectedValue = o.ToString();
                        return;
                    }
                    else
                    {
                        string item = drpT.SelectedItem.Text;
                        drpT.Items.Add(new ListItem(item, o.ToString()));
                        drpT.SelectedValue = o.ToString();

                    }

                }

            }
            catch (Exception)
            {
            }
        }
        #endregion

    }
}
